package com.nf.health.app.myfamily.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.FamilyGridAdapter;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.models.Family;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

/** 
 * @ClassName: MyFamilyActivity 
 * @Description: 我的家人
 * @author Ian 
 * @date 2015-11-4 上午9:57:52 
 *  
 */
public class MyFamilyActivity extends BaseActivity implements OnClickListener {
	private View rl_healthy_device;
	private View rl_old_age_account;
	private View rl_app_account;
	private View rl_cancel_bind;
	private GridView gridview;
	private List<Family> accounts;

	private LinearLayout linearLayout;

	private ImageView image_btn;

	public static final String TYPE_SIMPLE = "simple";
	public static final String TYPE_FAMILY = "family";
	public static final String TYPE_OLD_MOBILE = "oldMobile";

	private LinearLayout mGallery;
	private LayoutInflater mInflater;

	private TextView textview_tishi; 

	int i;

	View view;
	private int currentPos = -1;


	Boolean tt;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_my_family);

		ViewUtil.setText(this, R.id.titlebar_title_tv, "我的家人");
		rl_healthy_device = ViewUtil.getView(this, R.id.rl_healthy_device);
		rl_old_age_account = ViewUtil.getView(this, R.id.rl_old_age_account);
		rl_app_account = ViewUtil.getView(this, R.id.rl_app_account);
		rl_cancel_bind = ViewUtil.getView(this, R.id.rl_cancel_bind);
		image_btn = (ImageView) findViewById(R.id.image_btn);
		gridview = ViewUtil.getView(this, R.id.gridview);

		linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
		textview_tishi = (TextView) findViewById(R.id.textview_tishi);
		mInflater = LayoutInflater.from(this);

		initListener();


	}

	private void initView() {
		// TODO Auto-generated method stub
		mGallery = (LinearLayout) findViewById(R.id.id_gallery);
		mGallery.removeAllViews();

		if(accounts.size()>0 && accounts.size() < 4){
			tt = true;
			mGallery.setVisibility(View.VISIBLE);
			linearLayout.setVisibility(View.GONE);
		}else if(accounts.size() == 0){
			tt = false;
			linearLayout.setVisibility(View.GONE);
			mGallery.setVisibility(View.GONE);
		}else if(accounts.size() > 4){
			linearLayout.setVisibility(View.VISIBLE);
			image_btn.setVisibility(View.VISIBLE);
			textview_tishi.setVisibility(View.VISIBLE);
			tt = true;
		}

		for (i = 0; i < accounts.size(); i++)
		{

			view = mInflater.inflate(R.layout.activity_index_gallery_item,
					mGallery, false);
			CircleImageView img = (CircleImageView) view
					.findViewById(R.id.id_index_gallery_item_image);
			TextView txt = (TextView) view
					.findViewById(R.id.id_index_gallery_item_text);


			if(Family.TYPE_SIMPLE.equals(accounts.get(i).getType())){

				img.setImageResource(R.drawable.my_family_icon);

			}else if(Family.TYPE_FAMILY.equals(accounts.get(i).getType())){
				img.setImageResource(R.drawable.myfamily_equip);
			}else{
				img.setImageResource(R.drawable.myfamily_oldphone);
			}
			if(TextUtils.isEmpty(accounts.get(i).getNick())){
				txt.setText(accounts.get(i).getAccount());
			}else{
				txt.setText(accounts.get(i).getNick());
			}


			view.setTag(i);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					int pos = (Integer) arg0.getTag();
					currentPos = pos;
					Bundle bundle = new Bundle();
					bundle.putSerializable("member", accounts.get(currentPos));
					ActivityUtils.showActivity(MyFamilyActivity.this, FamilyMemberActivity.class, bundle);
				}
			});
			mGallery.addView(view);

		}

		image_btn.setOnClickListener(new OnClickListener() {

			@SuppressLint("ResourceAsColor")
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(tt){
					gridview.setVisibility(View.VISIBLE);
					mGallery.setVisibility(View.GONE);
					image_btn.setImageResource(R.drawable.shang_guanbi);
					linearLayout.setBackgroundColor(Color.WHITE);
					textview_tishi.setText("收起");
					textview_tishi.setTextColor(Color.BLACK);
					tt = false;

				}else{
					mGallery.setVisibility(View.VISIBLE);
					gridview.setVisibility(View.GONE);
					image_btn.setImageResource(R.drawable.xia_zhankai);
					linearLayout.setBackgroundColor(Color.parseColor("#22D5DA"));
					textview_tishi.setText("查看全部");
					textview_tishi.setTextColor(Color.WHITE);
					tt = true;
				}
			}
		});

	}

	@SuppressWarnings("unchecked")
	@Override
	public void handleActionSuccess(String actionName, Object object) {
		accounts = (List<Family>)object;

//		initView();

		int height = 0;
		int num = 0;
		if(accounts.size()>0){
			num = accounts.size()/5;
			if(accounts.size()%5==0){
				height = DensityUtil.dip2px(this, 95*num);
			}else{
				height = DensityUtil.dip2px(this, 95*(num+1));
			}
		}
		gridview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
		gridview.setAdapter(new FamilyGridAdapter(accounts));
	}

	private void initListener() {
		rl_healthy_device.setOnClickListener(this);
		rl_old_age_account.setOnClickListener(this);
		rl_app_account.setOnClickListener(this);
		rl_cancel_bind.setOnClickListener(this);

		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("member", accounts.get(position));
				ActivityUtils.showActivity(MyFamilyActivity.this, FamilyMemberActivity.class, bundle);
			}
		});
	}

	@Override
	public void onClick(View v) {
		Bundle bundle = new Bundle();
		switch (v.getId()) {
		case R.id.rl_healthy_device:
			bundle.putInt("type", AccountBindingActivity.BINDING_HEALTHY_DEVICE);
			ActivityUtils.showActivityForResult(this, AccountBindingActivity.class, bundle,100);
			break;
		case R.id.rl_old_age_account:
			bundle.putInt("type", AccountBindingActivity.BINDING_OLD_AGE_ACCOUNT);
			ActivityUtils.showActivity(this, AccountBindingActivity.class, bundle);
			break;
		case R.id.rl_app_account:
			bundle.putInt("type", AccountBindingActivity.BINDING_APP_ACCOUNT);
			ActivityUtils.showActivityForResult(this, AccountBindingActivity.class, bundle,100);
			break;
		case R.id.rl_cancel_bind:
			ActivityUtils.showActivityForResult(this, CancelBindingActivity.class,100);
			break;
		}
		super.onClick(v);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 100 && resultCode == RESULT_OK){
			provider.requestMyFamily("myFamily");
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		provider.requestMyFamily("myFamily");
	}

}
